package com.admin.action;

import com.admin.entity.Admin;
import com.admin.service.ServiceAdmin;
import com.uni.entity.Commodity;
import com.uni.entity.User;
import com.uni.service.Service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebListener;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * --------------project-------------
 *
 * @Author Unirithe  - Mr.Chao
 * - CSDN:
 * https://blog.csdn.net/Unirithe
 * - GitHub:
 * https://github.com/unirithe
 * @create 2021/12/28 20:10
 * ------------------------------------------
 */
@WebServlet(name = "AdminLoginAction", urlPatterns = "/toAdmin")
public class AdminLoginAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("userInfo");
        Admin admin = ServiceAdmin.admin.selectById(user.getId());
        List<User> users = Service.user.queryAllUser();
        List<Commodity> commodities = Service.commodity.queryAll();
        for (Commodity commodity : commodities) {
            commodity.setTypeName(Service.commodity.queryTypeById(commodity.getTid()));
            commodity.setUserName(Service.user.queryUserById(commodity.getUid()).getName());
        }
        session.setAttribute("userList", users);
        session.setAttribute("commodityList", commodities);
        resp.sendRedirect("admin.jsp");
    }
}
